//
//  HWTabBarController.m
//  94hwan
//
//  Created by 金石 on 2018/4/25.
//  Copyright © 2018年 孔祥刚. All rights reserved.
//



#import "HWTabBarController.h"
#import "HWFirstPageViewController.h" // 首页
#import "FenLeiViewController.h" // 分类
#import "HWTradingViewController.h" // 交易
#import "HWMyViewController.h"  // 我的
#import "HWGiftBagViewController.h" // 中间
#import "HWNavigationViewController.h"
#import "HWVideoshipingViewController.h"
#import "HWVideoshipingViewController.h" //视频
#import "HWFulijiemiamanViewController.h"
#import "MainMidddleBar.h"

#import "HWGameClassificationViewController.h"
#import "HWanGame-Swift.h"
@interface HWTabBarController ()<UITabBarControllerDelegate>

@property(nonatomic,strong)MainMidddleBar *mainTabBar; // 凸起发布按钮
@property (nonatomic,assign) NSInteger  indexFlag;　//记录上一次点击tabbar，使用时，记得先在init或viewDidLoad里 初始化 = 0
@property(strong,nonatomic) UIView *blankView;



@end

@implementation HWTabBarController
{
    NSMutableArray *_controllers;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    // self.navigationController.navigationBar.hidden = YES;
    
    
    
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    
    //    self.tabBar.backgroundColor = [UIColor whiteColor];
    //    [[UITabBar appearance] setBackgroundColor:UIColorFromRGB(0xffffff)];
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];    // Do any additional setup after loading the view.
    //    _controllers = [NSMutableArray arrayWithCapacity:6];
    self.mainTabBar = [[MainMidddleBar alloc] init];
    //中间自定义tabBar点击事件
    //    __weak __typeof(self) weakSelf = self;
    //    self.mainTabBar.btnClickBlock = ^(UIButton *btn) {
    //        __typeof(&*weakSelf) strongSelf = weakSelf;
    //        strongSelf.selectedIndex = 4;
    //    };
    //
    
    [self setValue:self.mainTabBar forKeyPath:@"tabBar"];
    
    //    //首页
    
    NSString *home =  NSLocalizedString(@"首页", nil);
    //    HWFirstPageViewController *firstPage = [[HWFirstPageViewController alloc] init];
    HWHomeViewController *firstPage = [[HWHomeViewController alloc] init];
    [self addControllerWithTitle:home imageName:@"home_home_a" selectImage:@"home_home_b" controller:firstPage];
    
    // 分类
    //    FenLeiViewController *fenLei = [[FenLeiViewController alloc] init];
    //    [self addControllerWithTitle:@"游戏" imageName:@"icon_home_classify_a" selectImage:@"icon_home_classify_b" controller:fenLei];
    
    
    
    HWGameClassificationViewController *fenLei = [[HWGameClassificationViewController alloc]init];
    
    [self addControllerWithTitle:[HWjianzhiduiObject keylocalization:@"游戏"] imageName:@"icon_home_classify_a" selectImage:@"icon_home_classify_b" controller:fenLei];
    
    
    if(@available(iOS 15.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor= [UIColor whiteColor];
        //        self.tabBar.standardAppearance= appearance;
        //        self.tabBar.scrollEdgeAppearance = self.tabBar.standardAppearance;
        [UITabBar appearance].standardAppearance = appearance;
        [UITabBar appearance].scrollEdgeAppearance = appearance;
    }
    // HWVideoshipingViewController
    
    
    
    HWVideoshipingViewController *shipin = [[HWVideoshipingViewController alloc]init];
    
    NSString *shgiping =   [HWjianzhiduiObject keylocalization:@"视频"];
    [self addControllerWithTitle:shgiping imageName:@"shiping" selectImage:@"shiupingSeled" controller:shipin];
    
    
    
    
    
    
    //    HWTradingViewController *Trading = [[HWTradingViewController alloc]init];
    //    [self addControllerWithTitle:@"交易" imageName:@"icon_home_deal__a" selectImage:@"icon_home_deal__b" controller:Trading];
    //
    
    
    HWFulijiemiamanViewController *fuli = [[HWFulijiemiamanViewController alloc]init];
    
    
    [self addControllerWithTitle:[HWjianzhiduiObject keylocalization:@"福利"] imageName:@"weixuanzhonfuli_gaoliang" selectImage:@"fuli_gaoliang" controller:fuli ];
    
    
    
    
    
    
    
    
    
    //    HWMyViewController *my = [[HWMyViewController alloc]init];
    HWMineViewController *my = [[HWMineViewController alloc] init];
    //    hwminev
    NSString *wode =   [HWjianzhiduiObject keylocalization:@"我的"];
    
    [self addControllerWithTitle:wode imageName:@"icon_home_personal_a" selectImage:@"icon_home_personal_b" controller:my];
    
    
    //兑换 中间位置
    //    HWGiftBagViewController *packageControl = [[HWGiftBagViewController alloc] init];
    //    [self addControllerWithTitle:@"礼包" imageName:@"list" selectImage:@"list" controller:packageControl];
    //
    
    
    
    UIImage *whiteImage = [self imageWithColor:[UIColor whiteColor]];
    if (@available(iOS 13.0, *)) {
        // 手动放一张白色底图遮住系统tabbar的顶部线条
        // blankView颜色必须设置
        //   UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, UIScreen.mainScreen.bounds.size.width, 0.5)];
        
        UIView *blankView = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, UIScreen.mainScreen.bounds.size.width, 0.5)];
        blankView.backgroundColor = UIColor.whiteColor;
        [self.mainTabBar addSubview:blankView];
        self.blankView = blankView;
        
        
        
        
    } else {
        [self.mainTabBar setBackgroundImage:whiteImage];
        [self.mainTabBar setShadowImage:whiteImage];
    }
    
    
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    
    
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    
    
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    CGContextSetFillColorWithColor(context, color.CGColor);
    
    
    CGContextFillRect(context, rect);
    
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    
    UIGraphicsEndImageContext();
    
    
    return image;
    
    
}



//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
//    if (tabBarController.selectedIndex == 4 || tabBarController.selectedIndex == 2) {
//        if (![HWUserModel loadData]) {
//            HWLoginViewController *loginVC = [HWLoginViewController new];
//            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
//            [tabBarController presentViewController:nav animated:NO completion:nil];
//        }
//
//    }
//}
//禁止tab多次点击
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    UIViewController *tbselect=tabBarController.selectedViewController;
    if([tbselect isEqual:viewController]){
        return NO;
    }
    return YES;
}




/**
 添加进tabbar
 
 @param title 标题
 @param imageName 图片名称
 @param controller 控制器对象
 */

- (void)addControllerWithTitle:(NSString *)title imageName:(NSString *)imageName selectImage:(NSString *)selectImage controller:(UIViewController *)controller {
    HWNavigationViewController *nav = [[HWNavigationViewController alloc] initWithRootViewController:controller];
    controller.tabBarItem.image = [UIImage imageNamed:imageName];
    controller.title = title;
    self.tabBar.tintColor = UIColorFromRGB(0x139cea);
    
    self.tabBar.tintColor = QCchunseshensetupian;
    
    
    
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self addChildViewController:nav];
    
    
}

+ (void)initialize
{
    //tabbar去掉顶部黑线
    //    [[UITabBar appearance] setBackgroundImage:[UIImage new]];
    //    [[UITabBar appearance] setShadowImage:[UIImage new]];
    //    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
}
//-(void)viewDidLayoutSubviews{
//    [super viewDidLayoutSubviews];
//    [MainMidddleBar setTabBarUI:self.tabBar.subviews tabBar:self.tabBar topLineColor:[UIColor lightGrayColor] backgroundColor:self.tabBar.barTintColor];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


//1.花99美元申请一个苹果appID（99美元分为公司账号和个人账号，公司账号需要和苹果那边沟通）
//2. 申请那些公司账号，你得向苹果公司提供公司一些资质材料。主要介绍公司的材料。 苹果那边会打电话咨询你的。

//3.上传的时候需要在电脑上配置证书 ，苹果官网创建Bundle Identifie



-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    
    
//    MainMidddleBar *taba = (MainMidddleBar *)tabBar;
//    if (index == 2) {
//        
//        self.blankView.hidden = YES;
//        
//        taba.huisenView.hidden = NO;
//        
//        
//        
//        
//    } else {
//        
//        self.blankView.hidden = NO;
//        
//        taba.huisenView.hidden = YES;
//    }
//    
//    
//    if (index != self.indexFlag) {
//        //执行动画
//        NSMutableArray *arry = [NSMutableArray array];
//        for (UIView *btn in self.tabBar.subviews) {
//            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//                [arry addObject:btn];
//            }
//        }
//        //添加动画
//        //---将下面的代码块直接拷贝到此即可---
//        
//        
//        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        //速度控制函数，控制动画运行的节奏
//        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        animation.duration = 0.2;       //执行时间
//        animation.repeatCount = 1;      //执行次数
//        animation.removedOnCompletion = NO;
//        animation.fillMode = kCAFillModeForwards;           //保证动画效果延续
//        animation.fromValue = [NSNumber numberWithFloat:1.0];   //初始伸缩倍数
//        animation.toValue = [NSNumber numberWithFloat:1.15];     //结束伸缩倍数
//        // [[arry[index] layer] addAnimation:animation forKey:nil];
//        
//        [[arry[index] layer]  addAnimation:animation forKey:nil];
//        
//        
//        //移除其他tabbar的动画
//        for (int i = 0; i<arry.count; i++) {
//            if (i != index) {
//                [[arry[i] layer] removeAllAnimations];
//            }
//        }
//        
//        self.indexFlag = index;
//    }
}

@end
